package routers

import (
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
	"sanrenx.com/xyxf/app/handlers"
	"sanrenx.com/xyxf/app/routers/middleware/jwt"
	"sanrenx.com/xyxf/app/routers/middleware/sign"
	_ "sanrenx.com/xyxf/docs"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/setting"
)

// InitRouter initialize routing information
func InitRouter() *gin.Engine {
	var (
		appApiV2 *gin.RouterGroup
		//cmsApiV1 *gin.RouterGroup
	)

	r := gin.New()
	//使用中间件、
	//r.Use(logger.Setup()) //请求日志
	//r.Use(cros.Setup()) //跨域
	r.Use(gin.Recovery(), jwt.Guest())
	//404
	r.NoRoute(func(c *gin.Context) {
		appG := app.Gin{C: c}
		appG.Response(http.StatusNotFound, e.ERROR, "请求方法不存在")
	})

	//V2 版本api
	if setting.ServerSetting.RunMode == "dev" {
		appApiV2 = r.Group("/dev/app/v2")
		//cmsApiV1 = r.Group("/dev/cms/v2")
	} else {
		appApiV2 = r.Group("/app/v2")
		//cmsApiV1 = r.Group("/cms/v2")
	}

	// 开发模式提供swagger 文档
	if setting.ServerSetting.Debug {
		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	} else {
		//使用验签
		appApiV2.Use(sign.SetUp())
	}

	//=============认证登录 start==============//
	appApiV2.POST("/auth/mp", handlers.AuthMPV2) //小程序认证token
	//*************认证登录 end****************//

	//===================================公开 start===================================//
	appApiV2.GET("/oppos", handlers.OpposV2)     //运营位
	appApiV2.GET("/category", handlers.Category) //运营位

	//=============文章 start==============//
	appApiV2.GET("/article", handlers.ArticleGetPageV2)      //列表
	appApiV2.GET("/article/album", handlers.ArticleGetAlbum) //专辑
	//*************文章 end****************//

	//=============解惑 start==============//
	appApiV2.GET("/issue", handlers.IssueGetPage) //问题列表
	//*************解惑 end****************//

	//=============藏经 start==============//
	appApiV2.GET("/sutra", handlers.SutraGetPageV2)           //列表
	appApiV2.GET("/sutra/chapter", handlers.SutraGetChapters) //章回
	//*************藏经 end****************//

	//=============音乐 start==============//
	appApiV2.GET("/music/playlist", handlers.MusicPlaylistGetPageV2)        //歌单列表
	appApiV2.GET("/music/playlist/audio", handlers.MusicGetPlaylist)        //歌单/专辑/榜单 音频列表
	appApiV2.GET("/music/keyman/audio", handlers.MusicKeymanAudioGetPageV2) //音乐列表
	//*************音乐 end****************//

	//***********************************公开 end***********************************//

	//授权
	appApiV2.Use(jwt.Must())
	{

		//=============用户 start==============//
		appApiV2.GET("/user/info/details", handlers.UserGetDetailsV2) //音乐详情
		appApiV2.GET("/user/std/daily", handlers.UserStdDaily)        //日修行记录
		appApiV2.POST("/user/std/sign", handlers.UserSignToday)       //签到
		//*************用户 end****************//

		//=============文章 start==============//
		appApiV2.GET("/article/details", handlers.ArticleGetDetailsV2) //文章详情
		//*************文章 end****************//

		//=============解惑 start==============//
		appApiV2.GET("/issue/details", handlers.IssueGetDetails) //问题详情
		appApiV2.POST("/issue", handlers.IssuePost)              //解惑提问
		//*************解惑 end****************//

		//=============藏经 start==============//
		appApiV2.GET("/sutra/chapter/details", handlers.SutraGetChapterDetailsV2) //藏经章回详情
		//*************藏经 end****************//

		//=============音乐 start==============//
		appApiV2.GET("/music/playlist/details", handlers.MusicGetPlaylistDetailsV2) //歌单详情
		appApiV2.GET("/music/audio/details", handlers.MusicGetAudioDetailsV2)       //音乐详情
		//*************音乐 end****************//

	}

	return r
}
